<template>
	<view class="control">
		<view class="section-1">
			<view class="image">
				<image :src="data.cover"></image>
				<view class="num"> {{data.sold_num}}人已报名 </view>
			</view>
			<view class="detail">
				<view class="title">{{ data.name }}</view>
				<view class="tag-list flex-align">
					<view class="tag" v-for="(item, index) in spec" :key="index">{{
            item.name
          }}</view>
				</view>
			</view>
		</view>

		<view class="section-2">
			<view class="detail flex-between">
				<view class="flex-align">
					<view class="price-1">￥{{ price }}</view>
					<!-- <view class="price-2">{{ data.classNum }}节课</view> -->
				</view>
				<!-- <view class="price-3">剩余{{ chooseStock }}名额</view> -->
			</view>

			<view class="list-title" style="
          display: flex;
          align-items: center;
          justify-content: space-between;
		  
        " @click="addStudent">
				选择报名人员(已选{{ selectLength }})
				<!-- 剩余可选人员({{ choosenum }}) -->
				<u-icon name="plus" color="#3e80fc" label="添加"></u-icon>
			</view>
			<view style="
			  display: flex;
			  align-items: center;
			  justify-content: space-between; margin-top: 30rpx;
			  
			">

			</view>

			<view class="kong flex-align" v-if="studentList.length < 1">
				<image mode="aspectFit"
					src="https://jiangsudiantuo2022.oss-cn-hangzhou.aliyuncs.com/img/39da52a35b59c656097d7b52006f8479b6de7ba6.png">
				</image>
				<view>
					<view>报名学员资料为空</view>
					<view>请登记学员信息</view>
				</view>
			</view>
			<view class="list" v-if="studentList.length > 0">
				<scroll-view class="scroll-view_H" scroll-y="true" :style="{ height: app_height * 0.7 + 'rpx' }">
					<checkbox-group shape="circle">
						<label class="item flex-between" v-for="(item, index) in studentList" :key="index"  @click="radioChecked(index, item)">
							<view class="flex-align">
								<u--image width="50" height="50" shape="circle" :src="item.head_img">
								</u--image>
								<view class="info">
									<view class="name">{{ item.name }}</view>
									<view class="phone">{{ item.mobile }}</view>
								</view>
							</view>
							<view class="radioBox" style="padding-left: 34rpx">
								<checkbox color="#2f75fa" :value="item.id" 
									:checked="item.checked" :disabled="item.disabled" />
							</view>

						</label>
					</checkbox-group>
					<view style="width: 100%;height: 110rpx;">

					</view>
				</scroll-view>
			</view>
		</view>

		<!-- <view class="section-3 flex-center">
      <u-icon
        color="#4d8bfe"
        size="28"
        name="plus-circle-fill"
        @click="openAdd"
      ></u-icon>
      <text>添加报名人员</text>
    </view> -->
	<!-- train_enum_id == 1  短训培训班-->
	<!-- train_enum_id == 2 长训-->
	<!-- train_enum_id == 3 小班-->
		<view class="footer flex-align" v-if="data.train_enum_id == 1">
			<view class="btn" @click="doPay" v-if="selectLength >= choosenum">开始报名</view>
			<view class="btn btn-disbled" v-else>开始报名</view>
		</view>
		<view class="footer flex-align" v-if="data.train_enum_id == 2">
			<view class="btn" @click="doPay" v-if="selectLength >= choosenum">开始报名</view>
			<view class="btn btn-disbled" v-else>开始报名</view>
		</view>
		<view class="footer flex-align" v-if="data.train_enum_id == 3">
			<view class="btn" @click="doPay" v-if="selectLength === data.group_member_num">开始报名</view>
			<view class="btn btn-disbled" v-else>开始报名</view>
		</view>
	</view>
</template>

<script>
	const train = require("@/api/train/index.js");
	export default {
		data() {
			return {
				id: 0,
				data: {},
				price: "0.00",
				studentList: [],
				venue_name: "",
				venue_id: 0,
				checkedNum: 0,
				spec: [],
				chooseStock: 0,
				chooseStudents: [],
				students: [],
				app_height: 0,
				arrid: [],
				selectLength: 0,
				quantity: 0,
				choosenum: '',
				skucode: "",
				sku: '',
				applicantlist: [],
				applicant: '',
				train_enum_id: '',
				sku_id:''
			};
		},

		onLoad(options) {
			console.log(options,'2222')
			let _this = this;
			uni.getSystemInfo({
				success: (res) => {
					console.log("手机可用高度:" + res.windowHeight * 2 + "rpx");
					_this.app_height = res.windowHeight * 2;
				},
			});
			this.train_enum_id = options.train_enum_id
			if (this.train_enum_id == 1) {
				this.id = options.id;
				this.price = options.price;
				this.venue_name = options.venue_name;
				this.venue_id = options.venue_id;
				this.chooseStock = options.chooseStock;
				this.spec = JSON.parse(options.checkedSpec);
				this.sku = options.sku
				this.skucode = options.skucode
				this.sku_id = options.sku_id
			} else if (this.train_enum_id == 2) {
				this.id = options.id;
				this.chooseStock = options.stock;
				this.venue_name = options.venue_name;
				this.venue_id = options.venue_id;
				this.price = options.price_selling;
				this.sku_id = options.sku_id
				this.sku = options.sku
				this.skucode = options.skucode
			} else if (this.train_enum_id == 3) {
				// 小班
				this.id = options.id;
				this.chooseStock = options.less_num;
				this.venue_name = options.venue_name;
				this.venue_id = options.venue_id;
				this.price = options.price_selling;
				this.sku_id = options.sku_id
				this.sku = options.sku
				this.skucode = options.skucode
			}
			this.getTrainDetail();
		},
		onShow() {
			this.studentList = []
			this.students = [];
			this.checkedNum = 0;
			this.applicant = 0
			this.selectLength = 0
			this.applicantlist = []
			this.selectStudent();
		},
		onUnload() {
			this.studentList = []
			this.students = [];
			this.checkedNum = 0;
			this.applicant = 0
			this.selectLength = 0
			this.applicantlist = []
		},
		onHide() {
			let that = this;
			that.applicant = 0
			this.applicantlist = []
		},
		methods: {
			radioChecked(index, item) {
				// <!-- train_enum_id == 1  短训培训班-->
				// <!-- train_enum_id == 2 长训-->
				// <!-- train_enum_id == 3 小班-->
				console.log(index, item,'6666')
				if (this.train_enum_id != 3) {
					this.applicantlist = []
					item.checked = !item.checked
					this.selectLength = this.studentList.filter(item => item.checked).length || 0
					// let applicant = []
					this.studentList.forEach((el, idx) => {
						if (el.checked) {
							this.applicantlist.push(el.id)
						}
						
					})
					this.applicant = this.applicantlist.toString()
					// if (this.selectLength >= this.choosenum) {
					// 	this.studentList.forEach((el, idx) => {
					// 		if (idx == index) {
					// 			el.disabled = false
					
					// 		} else {
					// 			// el.disabled = true
					// 			if(el.checked){
					
					// 			} else {
					// 				el.disabled = true
					// 			}
					// 		}
					// 	})
					// } else {
					// 	this.studentList.forEach((el, idx) => {
					// 		el.disabled = false
					// 	})
					// }
					
				} else{
					this.applicantlist = []
					item.checked = !item.checked
					this.selectLength = this.studentList.filter(item => item.checked).length || 0
					this.studentList.forEach((el, idx) => {
						if (el.checked) {
							this.applicantlist.push(el.id)
						}
					})
					this.applicant = this.applicantlist.toString()
				}
			},

			async selectStudent() {
				let postData = {
					page: 1,
					pageSize: 10,
					openid: uni.getStorageSync('openid'),
					app_id: this.$https.weixinAppId,
					project_id: getApp().globalData.projectId,
				};
				let res = await train.selectStudent(postData);
				if (res.code == 1) {
					this.studentList = res.data.train;
				}
			},
			addStudent() {
				let field = JSON.stringify(this.data.field_ids);
				// uni.navigateTo({
				// 	url: "/pages_other/train/addInfo?field=" + field,
				// });
				uni.navigateTo({
					url: "/pages_other/train/addInfo?id=" + this.data.id,
				});
				// uni.navigateTo({
				// 	url: "/page_venues/pages/student-archives/add/add" ,
				// });
			},
			// openAdd() {
			//   let field = JSON.stringify(this.data.field_ids);
			//   uni.navigateTo({
			//     url: "/pages_other/train/addPreson?field=" + field,
			//   });
			// },
			// 获取培训班详情
			async getTrainDetail() {
				let postData = {
					train_id: this.id,
					app_id: this.$https.weixinAppId,
					project_id: getApp().globalData.projectId,
					sku: this.sku
				}
				let res = await train.getTrainDetail(postData);
				if (res.code == 1) {
					this.data = res.data.train;
					this.quantity = this.data.quantity
					this.choosenumlist()
				}
			},
			choosenumlist() {
				if (this.quantity >= this.chooseStock) {
					this.choosenum = this.chooseStock
				} else {
					this.choosenum = this.quantity
				}
			},
			doPay() {
				let ticketID = this.data.id;
				this.students = [];
				this.studentList.forEach((item) => {
					if (item.checked === true) {
						this.students.push(item);
					}
				});
				// console.log(this.students)
				uni.navigateTo({
					url: `/page_venues/pages/venuesDetail/ticketModule/makeOther?selectionArr=${encodeURIComponent(JSON.stringify(this.students))}` +
						"&ticketID=" +
						ticketID +
						"&pay_wait_time=" +
						this.data.pay_wait_time +
						"&windows=" +
						this.data.windows +
						"&venue_name=" +
						this.venue_name +
						"&skucode=" +
						this.skucode +
						"&sku_id=" +
						this.sku_id +
						"&sku=" +
						this.sku +
						"&venue_id=" +
						this.venue_id +
						"&applicant=" +
						this.applicant +
						"&goods_type=train" +
						"&spec=" +
						JSON.stringify(this.spec) +
						"&classNum=" +
						this.data.classNum +
						"&price=" +
						this.price +
						"&train_price=" +
						this.price * this.selectLength +
						"&students=" +
						JSON.stringify(this.students),
				});
			},
		},
	};
</script>

<style scoped lang="scss">
	.control {
		width: 100%;
		min-height: 100vh;
		background-color: #f2f2f2;

		.section-1 {
			width: 100%;
			padding: 20rpx 30rpx;
			box-sizing: border-box;
			background-color: #2f75fa;
			height: 182rpx;
			padding-bottom: 50rpx;
			position: relative;

			.image {
				width: 200rpx;
				height: 200rpx;
				position: absolute;
				z-index: 3;
				left: 30rpx;
				top: 20rpx;
				border-radius: 10rpx;
				overflow: hidden;

				image {
					width: 100%;
					height: 100%;
				}

				.num {
					width: 100%;
					position: absolute;
					bottom: 0;
					left: 0;
					text-align: center;
					background-color: rgba(0, 0, 0, 0.6);
					padding: 10rpx 0;
					font-size: 20rpx;
					color: #ffffff;
				}
			}

			.detail {
				margin-left: 236rpx;

				.title {
					font-size: 28rpx;
					font-weight: bold;
					color: #ffffff;
					margin-bottom: 15rpx;
				}

				.tag-list {
					.tag {
						font-size: 20rpx;
						font-weight: 500;
						color: #ffffff;
						padding: 5rpx 10rpx;
						margin-right: 10rpx;
						background-color: #4483fb;
						border-radius: 2px;
					}
				}
			}
		}

		.section-2 {
			border-radius: 20rpx 20rpx 0px 0px;
			background-color: white;
			padding: 20rpx 30rpx 200rpx 30rpx;
			transform: translateY(-30rpx);

			.detail {
				margin-left: 236rpx;

				.price-1 {
					color: #ff4105;
					font-size: 32rpx;
					font-weight: 700;
					margin-right: 12rpx;
				}

				.price-2 {
					color: orange;
					font-size: 20rpx;
				}

				.price-3 {
					color: orange;
					font-size: 20rpx;
				}
			}

			.list-title {
				font-size: 28rpx;
				font-weight: bold;
				color: #000000;
				margin-top: 60rpx;
			}

			.list {
				.scroll-view_H {}

				.item {
					margin-top: 30rpx;

					.info {
						margin-left: 20rpx;

						.name {
							font-size: 26rpx;
							font-weight: 500;
							color: #333333;
						}

						.phone {
							font-size: 24rpx;
							font-weight: 500;
							color: #bebebe;
							margin-top: 10rpx;
						}
					}
				}
			}
		}

		.section-3 {
			flex-direction: column;

			text {
				margin-top: 15rpx;
				font-size: 22rpx;
				font-weight: 500;
				color: #333333;
			}
		}

		.kong {
			width: 100%;
			margin-top: 40rpx;
			justify-content: center;
			font-size: 24rpx;
			font-weight: 500;
			color: #bed2fd;

			image {
				width: 250rpx;
				height: 240rpx;
				margin-right: 30rpx;
			}
		}

		.footer {
			position: fixed;
			width: 100%;
			background-color: white;
			bottom: 0;
			left: 0;
			padding: 20rpx 25rpx 20rpx 25rpx;
			box-sizing: border-box;

			.menu {
				display: flex;
				flex-direction: column;
				align-items: center;
				margin-right: 40rpx;

				image {
					width: 35rpx;
					height: 35rpx;
				}

				text {
					color: rgb(51, 51, 51);
					font-size: 24rpx;
					font-weight: 700;
					margin-top: 13rpx;
				}
			}

			.btn {
				flex: 1;
				color: rgb(255, 255, 255);
				font-size: 30rpx;
				padding: 26rpx 0;
				border-radius: 60rpx;
				background-color: #3e80fc;
				text-align: center;
				font-weight: 700;
			}

			.btn-disbled {
				flex: 1;
				color: rgb(255, 255, 255);
				font-size: 30rpx;
				padding: 26rpx 0;
				border-radius: 60rpx;
				background-color: #bebebe;
				text-align: center;
				font-weight: 700;
			}
		}

		.checkbox .wx-checkbox-input {
			border-radius: 30upx;
			/* 圆角 */
			width: 30upx;
			/* 背景的宽 */
			height: 30upx;
			/* 背景的高 */
			border: 1px solid #ccc;
			background: #eee;
		}

		/* 复选框禁用样式 */

		.checkbox .wx-checkbox-input-disabled {
			border-radius: 30upx;
			/* 圆角 */
			width: 30upx;
			/* 背景的宽 */
			height: 30upx;
			/* 背景的高 */
			background: #f3f3f3;
			border: 1px solid rgba(201, 201, 201, 1);
		}

		/* 复选框选中后样式 */

		.checkbox .wx-checkbox-input.wx-checkbox-input-checked {
			background: rgba(255, 129, 92, 1);
			border: 1px solid rgba(255, 129, 92, 1);
		}

		/*checkbox选中后图标样式 */

		.checkbox .wx-checkbox-input.wx-checkbox-input-checked::before {
			width: 30upx;
			height: 30upx;
			line-height: 30upx;
			text-align: center;
			font-size: 24upx;
			color: #fff;
			background: transparent;
			transform: translate(-50%, -50%) scale(1);
			-webkit-transform: translate(-50%, -50%) scale(1);
		}

		/deep/ checkbox .wx-checkbox-input {
			border-radius: 50% !important;
			color: #ffffff !important;
		}

		/deep/ checkbox .wx-checkbox-input.wx-checkbox-input-checked {
			color: #fff;
			background: #2851F3;
		}

		/deep/.wx-checkbox-input.wx-checkbox-input-checked {
			border: none !important;
		}

	}
</style>
